
var languageRu = "ru",
	languageEn = "en",
	curLanguage = languageRu;

function openSettingsPage(){
	openStarsPage("settings_page");
	//$.mobile.changePage( $("#settings_page") );
}

function isFirstGame(){
	return localStorage.getItem('first') != 'false';
}

function setNotFirstGame(){
	localStorage.setItem('first', 'false');
}

function setLanguage(lang){
	curLanguage = lang;
	if(curLanguage == languageEn){
		$(".language").each(function(){
			$(this).html($(this).attr("en"));
		});
		$(".ilanguage").each(function(){
			$(this).attr("src", $(this).attr("en"));
		});
		$("#rule_text_en").css("display", "block");
		$("#rule_text").css("display", "none");
		dictionary = dictionaryEn;
	}
}

function setEnLanguage(){
	setLanguage(languageEn);
}

function isEn(){
	return curLanguage == languageEn;
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

$(document).ready(function() {
	//setEnLanguage();
	//var language = getUrlVars()['language'];
	//if(language != 'ru' && language != 'RU')
	//	setEnLanguage();
	
	//$(".edit_settings").attr("readonly", "readonly");
	
	isSound = localStorage.getItem('sound') != 'false';
	if(!isSound) $(".sound_check[tag = sound]").addClass('active');
	
	wordsCount = localStorage.getItem('wordsCount');
	if(wordsCount == null) wordsCount = 6;
	$("#count_word").text(wordsCount);
	
	for (var i = 0; i < tours.length; i++) {
		var sec = localStorage.getItem('tourSec'+i);
		if(sec != null)
			tours[i].time = sec;
		$(".tourSec[tag = "+i+"]").text(tours[i].time);	
	}
	
	var volumeBack = localStorage.getItem('volumeBack');
	if(volumeBack != null){
		$("#volume_back").val(volumeBack);
		setBackMusicVolume(volumeBack/100);
	}
	
	var volumeSound = localStorage.getItem('volumeSound');
	if(volumeSound != null){
		$("#volume_sound").val(volumeSound);
		setSoundVolume(volumeSound/100);
	}	
	
	$("#settings_btn_next").click(function(){
		wordsCount = $("#count_word").text();
		localStorage.setItem('wordsCount', wordsCount);
		localStorage.setItem('volumeBack', $("#volume_back").val());
		localStorage.setItem('volumeSound', $("#volume_sound").val());
		for (var i = 0; i < tours.length; i++) {
			var sec = $(".tourSec[tag = "+i+"]").text();
			localStorage.setItem('tourSec'+i, sec);
			tours[i].time = sec;
		}
		openStarsPage("main_page");
		//$.mobile.changePage( $("#main_page") );
	});	
	/*
	$("#count_word").change(function(){
		console.log('change');
		var cur = parseInt($(this).val());
		if(	isNaN(cur) ) cur = 3;
		$(this).val(Math.max( Math.min(cur, 8), 3));
	});

	$(".tourSec").change(function(){
		var cur = parseInt($(this).val());
		if(	isNaN(cur) ) cur = 5;
		$(this).val(Math.max( Math.min(cur, 999), 5));
	});	
	*/
	$("#volume_back").change(function(){
		setBackMusicVolume($(this).val()/100);
	});

	$("#volume_sound").change(function(){
		setSoundVolume($(this).val()/100);
	});
	
	$("#count_word").click(function(){
		$("#count_word_select").css("display", "block");
	});
	
	$("#count_word_select .select").bind(isSupportTouch() ? "touchstart" : "mousedown", function(){   //click(function(){
		$("#count_word").text($(this).text());
	});
	
	$(".tourSec").click(function(){
		var tag = $(this).attr("tag");
		var select = $("#tourSec_select");
		var top = 0;
		switch (tag) {
		  case "0":
			top = retina ? 784 : 401;
			break;
		  case "1":
			top = retina ? 900 : 458;
			break;
		  case "2":
			top = retina ? 1010 : 514;
			break;
		}
		select.css("top", top);
		select.css("display", "block");
		select.attr("tag", tag);
	});
	
	$("#tourSec_select .select").bind(isSupportTouch() ? "touchstart" : "mousedown", function(){  //click(function(){
		var tag = $("#tourSec_select").attr("tag");
		$(".tourSec[tag = "+tag+"]").text($(this).text());
	});	
	
});
